<?
if (!defined('IN_RJ')) die("Error 404");
if($cmd[1]=="playlist" && is_numeric($cmd[2])){
$pl_id=$cmd[2];
$DB->query("update ".$conf['prefix']."playlist set pl_viewed=pl_played+1,pl_viewed_month=pl_played_month+1 where pl_id={$pl_id}");
$query="select * from ".$conf['prefix']."playlist where pl_id={$pl_id}";
$rq = $DB->query($query);
    if ($pl=$DB->fetch_row($rq)){
		 $ro= $tpl->get_tpl('playlist');

			$q=$DB->query("SELECT * FROM ".$conf['prefix']."media WHERE m_id IN ({$pl['pl_contents']})");
			if($DB->num_rows($q)){
				$html='';
				$t['row']=$tpl->get_block_from_str($ro,"list_song_pl.row",1);
				while($song=$DB->fetch_row($q)){
					if(!$player) $player=$func->get_player($song['m_id']);
					if(!$cur_id) $cur_id=$song['m_id'];
					$html .= $tpl->assign_vars($t['row'],
						array(
							'song.ID'	=> $song['m_id'],
							'song.NAME'	=> $func->unhtmlchars($song['m_title']),
						)
					);
				}
			}
			$pl_name=$func->unhtmlchars($pl['pl_name']);
			$ro = $tpl->assign_vars($ro,
				array(
					'pl.IMG'	=> $func->get_img("playlist",$pl_id),
					'pl.ID'		=> $pl_id,
					'pl.NAME'	=> $pl_name,
					'pl.CURRENT_ID' => $cur_id,
				)
			);	
			$ro=$tpl->assign_blocks_content($ro,array(
					'list_song_pl' => $html,
					'player'		=> $player,
				)
			);
	}
	$title_web_bar=$pl_name;
	$title_ascii_web_bar=$func->utf8_to_ascii($pl_name);
	$main_content=$ro;

}

if($cmd[1]=="playlist" && $cmd[2]=="guest"){
    if ($_SESSION['ses_playlist']!=''){
		 $ro= $tpl->get_tpl('playlist');
		 	$pl_contents=$_SESSION['ses_playlist'];
			$q=$DB->query("SELECT * FROM ".$conf['prefix']."media WHERE m_id IN ({$pl_contents})");
			if($DB->num_rows($q)){
				$html='';
				$t['row']=$tpl->get_block_from_str($ro,"list_song_pl.row",1);
				while($song=$DB->fetch_row($q)){
					if(!$player) $player=$func->get_player($song['m_id']);
					if(!$cur_id) $cur_id=$song['m_id'];
					$html .= $tpl->assign_vars($t['row'],
						array(
							'song.ID'	=> $song['m_id'],
							'song.NAME'	=> $func->unhtmlchars($song['m_title']),
						)
					);
				}
			}
			$ro = $tpl->assign_vars($ro,
				array(
					'pl.IMG'	=> $func->unhtmlchars($pl['pl_img']),
					'pl.NAME'	=> "",
					'pl.CURRENT_ID' => $cur_id,
				)
			);	
			$ro=$tpl->assign_blocks_content($ro,array(
					'list_song_pl' => $html,
					'player'		=> $player,
				)
			);
	}
	$title_web_bar=$lang['playlist'];
	$title_ascii_web_bar=$lang['playlist'];
	$main_content=$ro;

}
if($cmd[1]=="playlist" && $cmd[2]=="add"){
	$ext_arr = array(
		'gif',
		'jpg',
		'jpeg',
		'png',
	);
	if(isset($_POST['sb_add_pl'])){
		$pl_name=$func->htmlchars(trim($_POST['pl_name']));
		$up=$_FILES['file_img'];
		if(isset($up) && is_uploaded_file($up['tmp_name'])){
			$ext=$func->get_ext($up['name']);
			if (!in_array($ext,$ext_arr)){
				$warn.="<b>{$lang['error']}</b> : {$lang['not_support_this_file']}";
			}
			elseif($up['size']>204800)
				$warn.="<b>{$lang['error']}</b> : {$lang['exceed_size_allowed']} - 200kb";
			else{
			 $rename=time().".".$ext;
			//  $destination = $conf['dir_imgpl']."/".$rename;
			  $cache_file="cache/".$rename;
			  move_uploaded_file($up['tmp_name'], $cache_file);
			  	$func->img_resize( $cache_file ,150,150,$conf['dir_imgpl'],$rename);
				unlink($cache_file);
			  $pl_img=$rename;
		  }
		}
		if($warn){
			$julie=$tpl->get_tpl("add_playlist");	
			$julie=$tpl->assign_vars($julie,array(
					'add_pl.WARNING'	=> $warn,
					'add_pl.NAME'		=>$func->unhtmlchars($pl_name),
				)
			);
		}
		elseif($pl_name && $pl_img && !$warn){
			$qr=$DB->query("INSERT INTO {$conf['prefix']}playlist (pl_name,pl_img,pl_userid) VAlUES('{$pl_name}','{$pl_img}','".$_SESSION['ses_user_id']."')");
			if($qr)
				$warn=$lang['added'];
			else
				$warn=$lang['error'].", ".$lang['try_again'];

			$julie=$tpl->get_tpl("announcement");	
			$julie=$tpl->assign_vars($julie,array(
					'ann.CONTENT'	=> $warn,
				)
			);
		
		}	

		$main_content=$julie;
	}
	else{		
		$roju=$tpl->get_tpl("add_playlist");
			$roju=$tpl->assign_vars($roju,array(
					'add_pl.WARNING'	=> '',
					'add_pl.NAME'		=> '',
				)
			);
		$main_content=$roju;
	}
	$title_web_bar=$lang['add_playlist'];
	$title_ascii_web_bar=$func->utf8_to_ascii($lang['add_playlist']);
		
}
?>